package com.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.blog.common.R;
import com.blog.domain.User;
import com.blog.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @AUTHOR:HZG MADE IN CHINA
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService userService;


    /**
     * 新增user
     * @param user
     * @return
     */
    @PostMapping("/register")
    public R<User> save(@RequestBody User user){

        //进行md5加密处理
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        //根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User mUser = userService.getOne(queryWrapper);

        if (mUser != null){
            return R.error("用户已存在");
        }
        //保存数据集
        user.setAvatar("/user/avatar");
        userService.save(user);

        return R.success(user,"注册成功");
    }


    /**
     * user登录
     * @param request
     * @param user
     * @return
     */
    @PostMapping("/login")
    public R<User> login(HttpServletRequest request, @RequestBody User user){

        //1、将页面提交的密码password进行md5加密处理
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User mUser = userService.getOne(queryWrapper);

        //3、如果没有查询到则返回登录失败结果
        if(mUser == null){
            return R.error("用户不存在");
        }

        //4、密码比对，如果不一致则返回登录失败结果
        if(!mUser.getPassword().equals(password)){
            return R.error("密码错误");
        }

        //5、登录成功，将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("muserId",mUser.getId());
        mUser.setPassword("");
        return R.success(mUser,"登录成功");
    }

    /**
     * 修改用户密码
     * @param user
     * @return
     */

    @PutMapping("/{oldpassword}")
    public R<String> updatePassword(@RequestBody User user,@PathVariable String oldpassword){

        //将页面提交的密码oldpassword进行md5加密处理
        oldpassword = DigestUtils.md5DigestAsHex(oldpassword.getBytes());

        //根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User mUser = userService.getOne(queryWrapper);
        //将页面提交的user的password进行md5加密处理
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password);

        if (mUser == null) {
            return R.error("用户不存在，请注册账号");
        }

        if(!mUser.getPassword().equals(oldpassword)){
            return R.error("密码错误");
        }

        mUser.setPassword(user.getPassword());
        userService.updateById(mUser);

        return R.success("密码修改成功");
    }

    /**
     * 修改用户头像
     * @param user
     * @return
     */

    @PutMapping("/modifyavatar")
    public R updateAvatar(@RequestBody User user){

        //根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User mUser = userService.getOne(queryWrapper);

        if (mUser == null) {
            return R.error("用户不存在，请注册账号");
        }
        mUser.setAvatar(user.getAvatar());
         userService.updateById(mUser);

        return R.success(user.getAvatar(),"修改成功");
    }
    /**
     * 修改用户信息
     * @param user
     * @return
     */

    @PutMapping("/modify")
    public R update(@RequestBody User user){

        //根据页面提交的用户名id查询数据库
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,user.getId());
        User mUser = userService.getOne(queryWrapper);


        //根据页面提交的用户名查询数据库
        LambdaQueryWrapper<User> mqueryWrapper = new LambdaQueryWrapper<>();
        mqueryWrapper.eq(User::getUsername,user.getUsername());
        User muser = userService.getOne(mqueryWrapper);
        if (mUser == null ) {
            return R.error("用户不存在，请注册账号");
        }
        if (!user.getUsername().equals(mUser.getUsername())&&muser != null){
             return R.error("用户名已存在");
        }
        user.setId(mUser.getId());
        user.setAvatar(mUser.getAvatar());
        userService.updateById(user);

        return R.success(user,"修改成功");
    }
}
